/**
 * 
 */
package com.xidian.zhiancloud.util.tools;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.github.benmanes.caffeine.cache.Cache;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
/**
 * @author zwt
 *
 * 2021年3月22日
 * 阿里云短信服务：
 * 注意：需要 签名名称、模版CODE 以及 RAM访问控制中的 AccessKeyID 和 AccessKeySecret  
 */
@Component
public class AliyunSmsUtils {
 
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    
    static final String signname ="西安科航智联电子科技";
 
    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "LTAI4G8ZfwmDnKKUGV5gVf4C";  // TODO 修改成自己的
    static final String accessKeySecret = "lL30R5GNNLdlCB1W9YoJuwRvrihxwU";   // TODO 修改成自己的
    
    @Autowired
	private  Cache<String, Object> caffeineCache;
    
    private static AliyunSmsUtils aliyun;
    
    @PostConstruct
    public void init() {
    	aliyun = this;
    	aliyun.caffeineCache = this.caffeineCache;
    }
 
    public static SendSmsResponse sendSms(String telephone, String code,String template) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(telephone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signname);    
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(template);   
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
            System.out.println("短信发送成功！");
            aliyun.caffeineCache.put(telephone,code);
        }else {
            System.out.println("短信发送失败！");
            throw new CustomizeException(GeneralResponseEnums.SEND_FAILED);
        }
        return sendSmsResponse;
    }
    
    
    public static SendSmsResponse sendSms2(String telephone, String name,String template) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(telephone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signname);    
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(template);   
        request.setTemplateParam("{\"name\":\"" + name + "\"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
            System.out.println("短信发送成功！");
            
        }else {
            System.out.println("短信发送失败！");
            throw new CustomizeException(GeneralResponseEnums.SEND_FAILED);
        }
        return sendSmsResponse;
    }
    
    public static String sendCode(String telephone,String type) throws ClientException {
    	String template = null;
    	if(type != null && type.equals("1")) {  //重置密码
    		template="SMS_208636703";
    	}else  {
    		template="SMS_208641663";  //登录验证吗
    	}
    	setNewcode();
    	String code = Integer.toString(getNewcode());
    	System.out.println("code:"+code);
    	 SendSmsResponse response =sendSms(telephone,code,template);
    	 
    	return response.getCode();
    }
    
    public static String sendWarn(String telephone, String name,String type) throws ClientException {
    	String template = null;
    	if(type != null && type.equals("1")){   //排查计划
    		template="SMS_207370014";
    	}else{  //隐患排查
    		template="SMS_207365032";
    	}
    	 SendSmsResponse response =sendSms2(telephone,name,template);
    	 if(response !=null && response.getCode().equals("OK")) {
    		 
    	 }
    	return response.getCode();
    }
    
    public static String checkCode(String telephone) {
    	return  (String) aliyun.caffeineCache.asMap().get(telephone);
    }
    
 
    //以下为测试代码，随机生成验证码
    private static int newcode;
    public static int getNewcode() {
        return newcode;
    }
    public static void setNewcode(){
        newcode = (int)(Math.random()*9999)+100;  //每次调用生成一位四位数的随机数
    }
    
    public static void main(String[] args) throws ClientException, InterruptedException {
        setNewcode();
        String code = Integer.toString(getNewcode());
        System.out.println("发送的验证码为："+code);
        //发短信
       // SendSmsResponse response =sendSms2("17722427726","周伟涛","0"); // TODO 填写你需要测试的手机号码
        sendWarn("17722427726","周伟涛","0");
        System.out.println("短信接口返回的数据----------------");
        //System.out.println("Code=" + response.getCode());
       
    }
}